<?php

namespace mpend\modules\home\models;

use common\helpers\ArrayHelper;
use common\models\actor\Actor;
use Yii;
use common\helpers\Url;
use common\logics\PositionLogic;
use mpend\models\searchs\AssembleProductSearch;
use common\models\notice\NoticeTag;
use common\models\notice\Notice;
use common\models\notice\NoticeNeed;
use common\models\notice\ShowbizRole;
use yii\db\Expression;

/**
 * 推荐内容查询模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class RecommendSearch extends \common\base\Model {

    public $user_id;

    public function recommenders($length = 4) {
        $times = microtime(true);
        //公告关键词
        $query = Notice::find()->alias('t');
        $query->joinWith('roles roles');
        $query->andWhere([
            't.user_id' => $this->user_id
        ]);
        $query->select([
            't.id',
            new Expression('GROUP_CONCAT(`roles`.`name`) roleNames'),
        ])->groupBy([
            't.id'
        ]);
        $query->orderBy([
            'roles.id' => SORT_ASC
        ]);
        $data = $query->asArray()->all();
        $temp = [];
        $keywords = [];
        foreach ($data as $item) {
            $words = explode(',', $item['roleNames']);
            sort($words);
            $reals = implode(',', $words);
            if (isset($temp[$reals])) {
                $temp[$reals]++;
            } else {
                $temp[$reals] = 0;
            }
            foreach ($words as $word) {
                if (isset($keywords[$word])) {
                    $keywords[$word]++;
                } else {
                    $keywords[$word] = 1;
                }
            }
        }
        $likes = array_filter($temp);
        return $this->queryActor($likes, $keywords);
    }

    public function queryActor($likes, $keywords) {
        $actorLists = [];
        $limits = 4;
        $ids = [];
        $baseQuery = Actor::find()->alias('t')->active();
        $baseQuery->joinWith('tags tags');
        $i = 0;
        foreach ($likes as $tags => $c) {
            $query = clone $baseQuery;
            $likestring = '%' . str_replace(',', '%', $tags) . '%';
            $query->andFilterWhere([
                'not in', 't.id', $ids
            ]);
            $query->select([
                't.*',
                new Expression('GROUP_CONCAT(`tags`.`text` ORDER BY convert(`tags`.`text` using gbk) ASC) AS `tagnames`'),
            ]);
            $query->groupBy('t.id');
            $query->having([
                'LIKE', 'tagnames', $likestring, false
            ]);
            $query->orderBy([
                't.weight' => SORT_DESC
            ]);
            $query->limit($limits);
            $actors = $query->all();
            $ids = ArrayHelper::merge($ids, ArrayHelper::getColumn($actors, 'id', []));
            $actorLists[] = $actors;
            $sizes[] = 4 * $i;
        }
        foreach ($keywords as $word => $times) {
            $query = clone $baseQuery;
            $query->andFilterWhere([
                'not in', 't.id', $ids
            ]);
            $query->andWhere([
                'tags.text' => $word
            ]);
            $query->groupBy('t.id');
            $query->orderBy([
                't.weight' => SORT_DESC
            ]);
            $query->limit($limits);
            $actors = $query->all();
            $ids = ArrayHelper::merge($ids, ArrayHelper::getColumn($actors, 'id', []));
            $actorLists[] = $actors;
            $sqls[] = $query->debug();
            $sizes[] = 4 * $i;
        }
        $actors = [];
        $temps = $actorLists;
        while (!empty($temps) && count($actors) < $limits) {
            foreach ($temps as $key => $items) {
                if (empty($items)) {
                    unset($temps[$key]);
                    continue;
                }
                $actors[] = array_shift($items);
                $temps[$key] = $items;
                if (count($actors) >= $limits) {
                    break;
                }
            }
        }
        return $actors;
    }

}
